package com.jy.show.controller;

import com.jy.show.bean.City;
import com.jy.show.common.ResultCode;
import com.jy.show.common.ResultCommon;
import com.jy.show.service.CityService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class CityController {

    @Autowired
    private CityService cityService;

    @PostMapping("/city")
    @ResponseBody
    @ApiOperation("插入城市疫情信息")
    public ResultCommon insertCity(City city){
        ResultCommon result = null;
        int i = cityService.insertCity(city);
        if (i==1){
            result = ResultCommon.success(ResultCode.SUCCESS,"插入成功");
            return result;
        }else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }

    @GetMapping("/city")
    @ResponseBody
    @ApiOperation("查询所有城市信息")
    public ResultCommon queryAll(){
        ResultCommon result = null;
        List<City> cities = cityService.queryAll();
        result = ResultCommon.success(ResultCode.SUCCESS,cities);
        return result;
    }

    @PutMapping("/city")
    @ResponseBody
    @ApiOperation("更新城市信息")
    public ResultCommon updateCity(City city){
        ResultCommon result = null;
        int i = cityService.updateCity(city);
        if (i==1){
            result = ResultCommon.success(ResultCode.SUCCESS,"更新成功");
            return result;
        }else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }

    @DeleteMapping("/city")
    @ResponseBody
    @ApiOperation("删除城市信息")
    public ResultCommon deleteCity(int id){
        ResultCommon result = null;
        int i = cityService.deleteCity(id);
        if (i==1){
            result = ResultCommon.success(ResultCode.SUCCESS,"删除成功");
            return result;
        }else {
            result = ResultCommon.fail(ResultCode.FAIL);
            return result;
        }
    }
}
